/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.bean.bo;

import java.util.Date;

import lombok.experimental.Accessors;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import com.hyts.infodb.enums.SystemConfiguration;

import lombok.Data;

/** 
 * @author LiBo/Alex
 * @since V1.0
 * @version V1.0 
 */
@Data
@Accessors(chain=true)
public class BaseBO extends CondtionBO{
    
    /**  
     * @fieldName serialVersionUID
     * @fieldType long
     */ 
    private static final long serialVersionUID = -1744593273915854558L;

    /**  
     * <p>主键ID</p> 
     * @fieldName id
     * @fieldType Long
     */ 
    private Long id;
    
    /**  
     * <p>创建时间</p> 
     * @fieldName createDate
     * @fieldType Date
     */ 
    private Date createDate;
    
    /**  
     * <p>修改时间</p> 
     * @fieldName editDate
     * @fieldType Date
     */ 
    private Date editDate;
    
    /**  
     * <p>分页参数</p> 
     * @fieldName pageData
     * @fieldType Pageable
     */ 
    private PageRequest pageData;
    
    /**  
     * <b>page 数据信息</b>
     * @param pageNo
     * @param pageSize
     * @return
     * @exception
     */ 
    public PageRequest buildPageData(Integer pageNo,Integer pageSize) {
        int defaultPageSize = (int) SystemConfiguration.PageConfiguration.PAGE_SIZE.getValue();
        int defaultPageNum = (int) SystemConfiguration.PageConfiguration.PAGE_NUM.getValue();
        return PageRequest.of(pageNo == null ?defaultPageNum:pageNo - 1,pageSize == null?defaultPageSize:pageSize);
    }
    
   
    /**  
     * <b>page 数据信息</b>
     * @param pageNo
     * @param pageSize
     * @param sort 
     * @return
     * @exception
     */ 
    public PageRequest buildPageData(Integer pageNo,Integer pageSize,Sort sort) {
        int defaultPageSize = (int) SystemConfiguration.PageConfiguration.PAGE_SIZE.getValue();
        int defaultPageNum = (int) SystemConfiguration.PageConfiguration.PAGE_NUM.getValue();
         if(sort != null) {
                pageData = PageRequest.of(pageNo == null ?defaultPageNum:pageNo - 1,pageSize == null?defaultPageSize:pageSize);

         }else {
             pageData = buildPageData(pageNo,pageSize);
         }
        return pageData;
    }
    
}
